
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yybb_flutter/Macros/yyb_macros.dart';
import 'package:yybb_flutter/http/api_response.dart';
import 'package:yybb_flutter/modules/incremental_board/models/yybbincrementailCuleMode.dart';
import 'package:yybb_flutter/modules/incremental_board/vmodels/incrementai_broad_vmodel.dart';
import 'package:yybb_flutter/yybbtools/yybb_loading_nodata_widget.dart';
import 'package:yybb_flutter/yybbtools/yybb_services.dart';
import 'package:yybb_flutter/yybbtools/yybb_tools.dart';

class IncrementaiSearchCustomerPage extends StatefulWidget {
  @override
  _IncrementaiSearchCustomerPageState createState() => _IncrementaiSearchCustomerPageState();
}

class _IncrementaiSearchCustomerPageState extends State<IncrementaiSearchCustomerPage> {

  List _dataList = [];
  var _page = 1;
  RefreshController _refreshController =  RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
    _getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: YYBBColor245,
      body: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: YYBBRefreshHeadWidget(),
        footer: YYBBRefreshLoadingWidget(),
        controller: _refreshController,
        onRefresh: _onRefresh,
        onLoading: _onLoading,
        child: _childWitdget(),
      ),
    );
  }

    //  loading 加载
    Widget _childWitdget() {
      Widget _childWidget;
      if (_dataList != null && _dataList.length != 0) {
        _childWidget = ListView.builder(
          itemBuilder: _cellForRow,
          itemCount: _dataList.length > 0 ? _dataList.length : 0,
        );
      } else {
        _childWidget = new YYBBNODataWidget();
        // YYBBPlatform.invokeMapMethod("NodataCallBack");
      }
      return _childWidget;
    }

    // Cell 布局
    Widget _cellForRow(BuildContext context, int index ) {
      YYBBIncrementailCuleModeList _model = _dataList[index];
      List labelNames = _model.labelNames.split(",");
      return FlatButton(
        splashColor: Colors.transparent, // 点击时的高亮效果设置为透明
        onPressed: (){
          _pushOrderDetailPage(_model);
        },
        child: Container(
          margin: EdgeInsets.fromLTRB(0, 12, 0, 0),
          constraints: BoxConstraints(
            minHeight: 10,
          ),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(4),
              ///圆角
              border: Border.all(color: Colors.white, width: 1)
            ///边框颜色、宽
          ),
          // color: Colors.white,
          child: Container(
            padding:EdgeInsets.fromLTRB(8, 12, 8, 12),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  _model.shortName != null?_model.shortName : _model.epName,
                  textAlign: TextAlign.left ,
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w700,
                    color: YYBBColor51,
                  ),
                ),
                SizedBox(height: 7,),
                Row(
                  mainAxisAlignment:MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "所在区域   ${_model.addressArea}",
                      textAlign: TextAlign.left,
                      style: TextStyle(
                        fontSize: 12,
                        color: YYBBColor119,
                      ),
                    ),
                    SizedBox(height: 8,),
                    Text(
                      "客户距离   ${_model.distance}km",
                      textAlign: TextAlign.left,
                      style: TextStyle(
                        fontSize: 12,
                        color: YYBBColor119,
                      ),
                    ),
                  ],
                ),
                SizedBox(height: 7,),
                Container(
                  height: 0.5,
                  color: YYBBLineColor,
                ),
                SizedBox(height: 10,),
                Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        "客户增量   ${_model.increment}万㎡",
                        textAlign: TextAlign.left ,
                        style: TextStyle(
                          fontSize: 13,
                          // fontWeight: FontWeight.w200,
                          color: YYBBColor51,
                        ),
                      ),
                      Text(
                        "客户渗透率  ${_model.permeability}%",
                        textAlign: TextAlign.left ,
                        style: TextStyle(
                          fontSize: 13,
                          fontWeight: FontWeight.w600,
                          color: YYBBColor51,
                        ),
                      ),
                    ]
                ),
                SizedBox(height: 10,),
                Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        "实际采购量   ${_model.purchase}万㎡",
                        textAlign: TextAlign.left ,
                        style: TextStyle(
                          fontSize: 13,
                          // fontWeight: FontWeight.w200,
                          color: YYBBColor51,
                        ),
                      ),
                      Text(
                        "总采购规模  ${_model.visitPurchase}万㎡",
                        textAlign: TextAlign.left ,
                        style: TextStyle(
                          fontSize: 13,
                          fontWeight: FontWeight.w600,
                          color: YYBBColor51,
                        ),
                      ),
                    ]
                ),
                SizedBox(height: 10,),
                Text(
                  "客户标签",
                  textAlign: TextAlign.left ,
                  style: TextStyle(
                    fontSize: 13,
                    // fontWeight: FontWeight.w200,
                    color: YYBBColor51,
                  ),
                ),
                SizedBox(height: 10,),
                Container(
                  // padding: EdgeInsets.all(10.0),
                  child: Wrap(
                    spacing: 15.0,
                    runSpacing: 8.0,
                    //副轴间距
                    alignment: WrapAlignment.start,
                    //主轴上的对齐方式
                    crossAxisAlignment: WrapCrossAlignment.center,

                    //副轴上的对齐方式
                    children: List<Widget>.generate(
                      labelNames.length,
                          (int index) {
                        return _buildItem(index, labelNames);
                      },
                    ).toList(),
                  ),
                ),
              ],
            ),
          ),
        ),
      );
    }


  Widget _buildItem(int index,List list) {
    String content = list[index];
    return Chip(
      backgroundColor: Color.fromRGBO (202, 208, 220, 1),
      label: Text(
        content,
        style: TextStyle(fontSize: 12.0),
      ),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(3.0),
      ),
      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
      labelStyle: TextStyle(
        color: Color.fromRGBO (110, 136, 191, 1),
        fontWeight: FontWeight.w700,
      ),
    );
  }



    // 获取数据
    void _getData() async {
      var params = Map<String, String>();
      params["currentPage"] = _page.toString();
      params["pageSize"] = "20";
      ApiResponse<YYBBIncrementailCuleMode> item  = await IncrementaiBroadVmodel.getOldCustomerData(params);
      if(_page == 1) {
        _dataList.clear();
      }
      _dataList.addAll(item.data.list);
      _stopRefresh();
      // // 刷新界面
      setState(() {});
    }


    // 点击进入下一个界面
    void _pushOrderDetailPage(YYBBIncrementailCuleModeList model){
      var params = Map<String, String>();
      params["customerId"] = model.customerId.toString();
      params["enterpriseId"] = model.enterpriseId.toString();
      params["sellerEnterpriseId"] = model.sellerEnterpriseId.toString();
      YYBBPlatform.invokeMethod("jumpCustomerPortraitVC", params);

    }

    //下拉加载
    void _onRefresh() async{
      // await Future.delayed(Duration(milliseconds: 1000));
      _page = 1;
      _getData();
      setState(() {

      });

    }

    //上拉加载
    void _onLoading() async{
      // monitor network fetch
      // await Future.delayed(Duration(milliseconds: 1000));
      _page++;
      _getData();
      if(mounted)
        setState(() {
        });
    }


    // 停止刷新
    void _stopRefresh(){

      if(_dataList.length  < 10 * _page){
        _refreshController.loadNoData();
      }else{
        _refreshController.loadComplete();
      }
      _refreshController.refreshCompleted();
    }

}


